local Msg = require("core.Msg")
local ItemDefine = require("bag.ItemDefine")
local ObjHuman = require("core.ObjHuman")
local Lang = require("common.Lang")
local Broadcast = require("broadcast.Broadcast")
local InnerDataManagerFL = require("innerdata.InnerDataManagerFL")
local InnerDataDefine = require("innerdata.Define")
local VipLogic = require("vip.VipLogic")
local VipDefine = require("vip.VipDefine")
local RoleFFI = require("role.RoleFFI")
local ShopHelp = require("shop.ShopHelp")
local ActivityLogicL = require("activity.ActivityLogicL")
local HtmlUtil = require("common.HtmlUtil")

function getMaxTimes(human)
  return ItemDefine.SMELT_MAX_TIMES + VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_SMELTTIMES)
end

function sendSmeltTimes(human)
  local sendMsg = Msg.gc.GC_YUANBAO_SMELT_TIMES
  sendMsg.totalTimes = getMaxTimes(human)
  sendMsg.smeltTimes = human.db.rmbSmeltTimes
  Msg.send(sendMsg, human.fd)
end

function sendRemainHelpTimes(human)
  local sendMsg = Msg.gc.GC_YUANBAO_SMELT_HELP_TIMES
  sendMsg.times = ItemDefine.SMELT_CAN_HELP_TIMES + VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_HELPSMELT) - human.db.rmbSmelthelpTimes
  Msg.send(sendMsg, human.fd)
end

function sendIsPut(human)
  local sendMsg = Msg.gc.GC_YUANBAO_SMELT_IS_PUT
  sendMsg.isPut = human.db.rmbSmeltIsPut
  Msg.send(sendMsg, human.fd)
end

--邀请帮助
function inviteHelp(human)
  if human.yuanbaoInvite and os.time() - human.yuanbaoInvite < 30 then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_INVITE_TOO_MUCH)
  end
  human.yuanbaoInvite = os.time()
  --发送公告，让别人帮助熔炼,三十秒发一次
  local tip = string.format(Lang.RMB_SMELT_INVITE_HELP, HtmlUtil.fontName(human.name, human._id), human._id)
  Broadcast.sendBroadcast2World(Lang.GROSSIP, tip)
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SMELT_INVITE, nil)
end
--帮助他人熔炼
function helpOther(human, uuid)
  if human.db.rmbSmelthelpTimes >= ItemDefine.SMELT_CAN_HELP_TIMES + VipLogic.getVipTimes(human, VipDefine.VIP_PRIVILEGE_HELPSMELT) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HELP_TIMES_FULL)
  end
  if RoleFFI.ffi_string(human.db.helpUuid) ~= "" then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_HAVE_AlREADY_HELP)
  end
  local info = {}
  info.helpName = human.name
  info.helpUuid = human._id
  info.helpHead = human.db.sex
  info.uuid = uuid
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SMELT_HELP, info)
end
--熔炼
function smelt(human)
  if human.db.rmbSmeltIsPut ~= 1 then
    return
  end
  if human.db.rmbSmeltTimes >= getMaxTimes(human) then
    return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_TIMES_NOT_ENOUGH)
  end
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SMELT_SUCCESS, nil)
end

local function canGetBindYuanbao(human)
  local times = human.db.yestdaySmeltTimes == 0 and human.db.rmbSmeltTimes or (human.db.yestdaySmeltTimes + 1)
  if times == 3 then
    return ItemDefine.SMELT_CAN_GET_BIND_3
  elseif times == 2 then
    return ItemDefine.SMELT_CAN_GET_BIND_2
  else
    return ItemDefine.SMELT_CAN_GET_BIND
  end
end

function onSmeltOk(human, oType, info)
  if info.type == 1 then
    info.cnt = info.cnt or 0
    human.db.rmbSmeltTimes = human.db.rmbSmeltTimes + 1
    local total = ItemDefine.SMELT_HELP_MORE * info.cnt + canGetBindYuanbao(human)
    ObjHuman.addBindYuanbao(human, total, "rmbSmelt")
    Broadcast.sendBroadcast(human, Lang.DOWN, string.format(Lang.RMB_SMELT_SUCCESS, total))
    sendSmeltTimes(human)
    human.db.rmbSmeltIsPut = 0
    human.db.yestdaySmeltTimes = 0
    sendIsPut(human)
    local str = string.format(Lang.RMB_SMELT_SUCCESS_BRO, HtmlUtil.fontName(human.name, human._id), total, human._id)
    Broadcast.sendBroadcast2World(Lang.UP + Lang.GROSSIP, str)
    ActivityLogicL.updateActivityValue(human, "yuanbao")         
  else
    human.db.rmbSmelthelpTimes = human.db.rmbSmelthelpTimes + 1
    ObjHuman.addBindYuanbao(human, ItemDefine.SMELT_HELP_REWARD, "rmbSmelt")
    Broadcast.sendBroadcast(human, Lang.DOWN + Lang.CHAT, Lang.RMB_SMELT_HELP_SUCCESS)
    human.db.helpUuid = ""
    sendRemainHelpTimes(human)
    ActivityLogicL.updateActivityValue(human, "help_yuanbao")        
  end
end

function putInto(human)
  if human.db.rmbSmeltTimes >= getMaxTimes(human) then
    if human.db.rmbSmeltTimes == 3 then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_TIMES_NOT_ENOUGH)
    elseif human.db.rmbSmeltTimes == 2 then
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_TIMES_NOT_ENOUGH_2)
    else
      return Broadcast.sendBroadcast(human, Lang.DOWN, Lang.RMB_SMELT_TIMES_NOT_ENOUGH_3)
    end
  end
  if not ObjHuman.checkYuanbaoEnough(human, ItemDefine.SMELT_NEED_YUANBAO * (human.db.rmbSmeltTimes + 1)) then
    return ShopHelp.onNoRMB(human)   
  end
  ObjHuman.decYuanbao(human, ItemDefine.SMELT_NEED_YUANBAO * (human.db.rmbSmeltTimes + 1), "rmbSmelt")
  human.db.rmbSmeltIsPut = 1
  sendIsPut(human)
  InnerDataManagerFL.Send2WInnerData(human, InnerDataDefine.INNERDATA_TYPE_SMELT_INVITE, nil)  
end

InnerDataManagerFL.Register(InnerDataDefine.INNERDATA_TYPE_SMELT_SUCCESS, onSmeltOk)
